package edu.cs.usask.ca.silver.plugin.rcp.handlers.util;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverPreferenceEntries;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;

public class LoadVensimDLLHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		FileDialog fileDialog = new FileDialog(Activator.getDefault()
				.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);

		fileDialog.setFileName(SilverBusinessLogicController
				.getPreferenceEntry(SilverPreferenceEntries.VENDLL32));

		fileDialog.setFilterExtensions(new String[] { "*.dll" });
		fileDialog
				.setFilterNames(new String[] { "Vensim DLL (usually VENDLL32.dll)" });
		fileDialog
				.setText("Load Vensim DLL to System Memory (and remember the DLL location):");

		String path = fileDialog.open();
		if (path != null) {
			SilverBusinessLogicController.setPreferenceEntry(
					SilverPreferenceEntries.VENDLL32, path);
			SilverBusinessLogicController.loadVensimDLL(path);
		}
		return null;
	}

}
